/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.entity;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author GiangLT
 */
@Entity
@Table(name = "KQXS", catalog = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Kqxs.findAll", query = "SELECT k FROM Kqxs k"),
    @NamedQuery(name = "Kqxs.findById", query = "SELECT k FROM Kqxs k WHERE k.id = :id"),
    @NamedQuery(name = "Kqxs.deleteById", query = "DELETE FROM Kqxs k WHERE k.id = :id"),
    @NamedQuery(name = "Kqxs.findByNgayQuay", query = "SELECT k FROM Kqxs k WHERE k.ngayQuay = :ngayQuay"),
    @NamedQuery(name = "Kqxs.findByTinhQuay", query = "SELECT k FROM Kqxs k WHERE k.tinhQuay = :tinhQuay"),
    @NamedQuery(name = "Kqxs.findByTinhBetweenDate", query = "SELECT k FROM Kqxs k WHERE k.tinhQuay = :tinhQuay and k.ngayQuay >= :startDate and k.ngayQuay <= :endDate ORDER BY k.ngayQuay DESC"),
    @NamedQuery(name = "Kqxs.findByPhamViBetweenDate", query = "SELECT k FROM Kqxs k WHERE k.phamVi = :phamVi and k.ngayQuay >= :startDate and k.ngayQuay <= :endDate ORDER BY k.ngayQuay DESC"),
    @NamedQuery(name = "Kqxs.findBetweenDate", query = "SELECT k FROM Kqxs k WHERE k.ngayQuay >= :startDate and k.ngayQuay <= :endDate ORDER BY k.ngayQuay DESC"),
    @NamedQuery(name = "Kqxs.findByTinhAndNgay", query = "SELECT k FROM Kqxs k WHERE k.tinhQuay = :tinhQuay and k.ngayQuayStr = :ngayQuayStr"),
    @NamedQuery(name = "Kqxs.findByPublicBool", query = "SELECT k FROM Kqxs k WHERE k.publicBool = :publicBool"),
    @NamedQuery(name = "Kqxs.findByGioKhoiTao", query = "SELECT k FROM Kqxs k WHERE k.gioKhoiTao = :gioKhoiTao"),
    @NamedQuery(name = "Kqxs.findByGioCapNhatCuoi", query = "SELECT k FROM Kqxs k WHERE k.gioCapNhatCuoi = :gioCapNhatCuoi"),
    @NamedQuery(name = "Kqxs.findByNgayQuayStr", query = "SELECT k FROM Kqxs k WHERE k.ngayQuayStr = :ngayQuayStr"),
    @NamedQuery(name = "Kqxs.findByPhamVi", query = "SELECT k FROM Kqxs k WHERE k.phamVi = :phamVi")})
public class Kqxs implements Serializable {
    
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    private Long id;
    @Column(name = "NGAY_QUAY")
    @Temporal(TemporalType.TIMESTAMP)
    private Date ngayQuay;
    @Size(max = 100)
    @Column(name = "TINH_QUAY")
    private String tinhQuay;
    @Column(name = "PUBLIC_BOOL")
    private Short publicBool;
    @Column(name = "GIO_KHOI_TAO")
    @Temporal(TemporalType.TIMESTAMP)
    private Date gioKhoiTao;
    @Column(name = "GIO_CAP_NHAT_CUOI")
    @Temporal(TemporalType.TIMESTAMP)
    private Date gioCapNhatCuoi;
    @Size(max = 20)
    @Column(name = "NGAY_QUAY_STR")
    private String ngayQuayStr;
    @Size(max = 100)
    @Column(name = "PHAM_VI")
    private String phamVi;
    @OneToMany(mappedBy = "idKqxs", cascade = CascadeType.PERSIST)
    private List<GiaiThuong> giaiThuongList;
    
    public Kqxs() {
    }
    
    public Kqxs(Long id) {
        this.id = id;
    }
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Date getNgayQuay() {
        return ngayQuay;
    }
    
    public void setNgayQuay(Date ngayQuay) {
        this.ngayQuay = ngayQuay;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        this.ngayQuayStr = sdf.format(ngayQuay);
    }
    
    public String getTinhQuay() {
        return tinhQuay;
    }
    
    public void setTinhQuay(String tinhQuay) {
        this.tinhQuay = tinhQuay;
    }
    
    public Short getPublicBool() {
        return publicBool;
    }
    
    public void setPublicBool(Short publicBool) {
        this.publicBool = publicBool;
    }
    
    public Date getGioKhoiTao() {
        return gioKhoiTao;
    }
    
    public void setGioKhoiTao(Date gioKhoiTao) {
        this.gioKhoiTao = gioKhoiTao;
    }
    
    public Date getGioCapNhatCuoi() {
        return gioCapNhatCuoi;
    }
    
    public void setGioCapNhatCuoi(Date gioCapNhatCuoi) {
        this.gioCapNhatCuoi = gioCapNhatCuoi;
    }
    
    public String getNgayQuayStr() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = dateFormat.parse(ngayQuayStr);
            dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            return dateFormat.format(date);
        } catch (ParseException ex) {
            LoggerFactory.getLogger(Kqxs.class.getName()).error("", ex);
            return ngayQuayStr;
        }
        
    }
    
    public void setNgayQuayStr(String ngayQuayStr) {
        this.ngayQuayStr = ngayQuayStr;
    }
    
    public String getPhamVi() {
        return phamVi;
    }
    
    public void setPhamVi(String phamVi) {
        this.phamVi = phamVi;
    }
    
    @XmlTransient
    public List<GiaiThuong> getGiaiThuongList() {
        return giaiThuongList;
    }
    
    public void setGiaiThuongList(List<GiaiThuong> giaiThuongList) {
        this.giaiThuongList = giaiThuongList;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }
    
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Kqxs)) {
            return false;
        }
        Kqxs other = (Kqxs) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("KQXS ");
        sb.append(this.tinhQuay);
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sb.append(ngayQuay == null ? "" : sdf.format(ngayQuay));
        sb.append("\r\n");
        if (giaiThuongList != null) {
            for (GiaiThuong giaiThuong : giaiThuongList) {
                sb.append(giaiThuong.toString());
                sb.append("\r\n");
            }
        }
        return sb.toString();
    }
    
    public String toHtml() {
        StringBuilder sb = new StringBuilder();
        sb.append("KQXS ");
        sb.append(this.tinhQuay);
        sb.append("<br/>");
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sb.append(ngayQuay == null ? "" : "Ngày " + sdf.format(ngayQuay));
        sb.append("<br/>");
        if (giaiThuongList != null) {
            for (GiaiThuong giaiThuong : giaiThuongList) {
                sb.append(giaiThuong.toString());
                sb.append("<br/>");
            }
        }
        return sb.toString();
    }
}
